#include <stdio.h>
#include <unistd.h>
#include <iot_gpio.h>
#include <iot_gpio_ex.h>
#include <iot_spi.h>
#include "oled.h"
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "gui.h"
#include "temp_humi.h"
#include "box_control.h"

#define OLED_TASK_STACK_SIZE 4096



/* task处理函数 */
void OLedTask(void * para)
{
    printf("OLED task \r\n");
    OLED_Init();               // 初始化OLED  
    OLED_Clear(0);  
    
    while(1){
        // 显示温度
        char tempStr[20];
        snprintf(tempStr, sizeof(tempStr), "Temp: %.2f C", temp);
        GUI_ShowString(0, 0, tempStr, 16, 0);

        // 显示湿度
        char humiStr[20];
        snprintf(humiStr, sizeof(humiStr), "Humi: %.2f %%", humi);
        GUI_ShowString(0, 20, humiStr, 16, 0);

        // 显示药盒状态
        char boxStr[20];
        snprintf(boxStr, sizeof(boxStr), "Box: %s", fan_sta ? "Open" : "Closed");
        GUI_ShowString(0, 40, boxStr, 16, 0);
        
        
        
        OLED_Display();
        usleep(1000);
    }
    
    return NULL;
}

/* 创建oled任务函数 */
void oled_demo(void)
{    
    int ret;
    oled_gpio_io_init();
    screen_spi_master_init(0); 
    osThreadAttr_t attr;
 
    attr.name = "OLedTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = OLED_TASK_STACK_SIZE;
    attr.priority = 20;
    if (osThreadNew((osThreadFunc_t)OLedTask, NULL, &attr) == NULL) {
        printf("[OLedExample] Failed to create OLedTask!\n");
    }
}

SYS_RUN(oled_demo);